package com.google.code.bbdsm.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import blackboard.base.BbList;
import blackboard.persist.PersistenceException;

import com.google.code.bbdsm.service.DataSourceManager;

public class SwitchBoardController extends MultiActionController {
	
	private Log log = LogFactory.getLog(getClass());
	
	private DataSourceManager dataSourceManager;

	public DataSourceManager getDataSourceManager() {
		return dataSourceManager;
	}

	public void setDataSourceManager(DataSourceManager dataSourceManager) {
		this.dataSourceManager = dataSourceManager;
	}

	public ModelAndView listDataSources(HttpServletRequest request, HttpServletResponse response) throws PersistenceException{
		log.debug("Switchboard controller initialising model and view");
		List dataSources = dataSourceManager.findAll();
		Map<String,BbList> objectCounts = dataSourceManager.getObjectCounts();
		
		ModelAndView mav = new ModelAndView();
		
		//Automatic name inference doesn't seem to work with BbList :(
		mav.addObject("dataSources", dataSources);
		mav.addObject("objectCounts", objectCounts);
		
		return mav;
		
		
	}
}
